package com.beijiteshop.shop.ui.order.fragment

import android.os.Bundle
import android.os.CountDownTimer
import android.util.Log
import android.view.View
import com.beijiteshop.libcommon.ext.databinding.bindView
import com.beijiteshop.shop.R
import com.beijiteshop.shop.app.PAY_ALIPAY
import com.beijiteshop.shop.app.PAY_WECHAT
import com.beijiteshop.shop.base.BaseContainerFragment
import com.beijiteshop.shop.databinding.FragmentOrderPayBinding
import com.beijiteshop.shop.model.api.response.ORDER_PAY_TYPE
import com.beijiteshop.shop.model.api.response.OrderPayBean
import com.beijiteshop.shop.ui.order.vm.OrderVM
import com.beijiteshop.shop.ui.pay.PayActivity
import com.blankj.utilcode.util.LogUtils
import org.koin.android.viewmodel.ext.android.viewModel
import java.text.SimpleDateFormat
import java.util.*

/**
 * 订单支付页面
 */
class OrderPayFragment : BaseContainerFragment(R.layout.fragment_order_pay) {

    companion object{

        const val BUNDLE_ORDER_NUMBER = "bundle_order_number"//订单号

        fun newInstance(orderNum: String): OrderPayFragment{
            return OrderPayFragment().apply {
                arguments = Bundle().apply {
                    putString(BUNDLE_ORDER_NUMBER,orderNum)
                }
            }
        }
    }

    private var mOrderNumber: String? = null
    private var mOrderPayBean: OrderPayBean? = null
    private val mOrderVM: OrderVM by viewModel()
    private val mBinding by bindView<FragmentOrderPayBinding>()
    private var mCountDownTimer: CountDownTimer? = null

    override fun onLazyLoad() {
        requestData()
    }

    override fun initView(view: View) {
        mOrderNumber = arguments?.getString(BUNDLE_ORDER_NUMBER)

        mBinding.orderPayTitleBar.titleBarTitleTv.text = resources.getString(R.string.order_pay)
    }

    override fun initData() {

    }

    override fun initEvent() {
        startObserve()

        mBinding.orderPayTitleBar.titleBarBackIv.setOnClickListener {
            close()
        }

        mBinding.orderPayAliCb.setOnCheckedChangeListener { buttonView, isChecked ->
            mBinding.orderPayWechatCb.isChecked = !isChecked
        }

        mBinding.orderPayWechatCb.setOnCheckedChangeListener { buttonView, isChecked ->
            mBinding.orderPayAliCb.isChecked = !isChecked
        }

        mBinding.orderPayBtn.setOnClickListener {
            if (mOrderNumber.isNullOrEmpty() || mOrderPayBean == null){
                return@setOnClickListener
            }
            if (mBinding.orderPayAliCb.isChecked || mBinding.orderPayWechatCb.isChecked){
                val url = generatePayUrl()
                url?.let {
                    PayActivity.start(requireContext(),it)
                    close()
                }
            }else{
                showToast(resources.getString(R.string.please_select_pay_way))
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mCountDownTimer?.cancel()
    }

    private fun startObserve(){
        mOrderVM.liveOrderPayInfo.observeKt {orderPay->
            mOrderPayBean = orderPay
            if (orderPay != null){
                when(orderPay.status){
                    ORDER_PAY_TYPE.TOPAY.status -> {
                        mBinding.orderInfo = orderPay.info
                        setCountDownTimer()
                    }
                    ORDER_PAY_TYPE.QUIT.status -> {
                        orderPay.url?.let {
                            PayActivity.start(requireContext(),it)
                            close()
                        }
                    }
                    ORDER_PAY_TYPE.CLOSE.status -> {
                        orderPay.url?.let {
                            PayActivity.start(requireContext(),it)
                            close()
                        }
                    }
                }
            }
        }
    }

    private fun requestData(){
        mOrderNumber?.let {
            mOrderVM.getOrderPayInfo(it)
        }
    }

    private fun generatePayUrl(): String?{
        if (mBinding.orderPayWechatCb.isChecked){
            return PAY_WECHAT + mOrderPayBean?.info?.bus_OrdersID
        }
        if (mBinding.orderPayAliCb.isChecked){
            return PAY_ALIPAY + mOrderPayBean?.info?.bus_OrdersID
        }
        return null
    }

    private fun setCountDownTimer(){
        val orderInfo = mOrderPayBean?.info
        orderInfo?.let {
            val timeStr = it.timeM+":"+it.timeS
            val dateformat = SimpleDateFormat("MM:ss")
            val date = dateformat.parse(timeStr)
            if (mCountDownTimer == null){
                mCountDownTimer = object :CountDownTimer(date.time,1000){
                    override fun onFinish() {
                        close()
                    }

                    override fun onTick(millisUntilFinished: Long) {
                        mBinding.orderPayCountTimeTv.text = dateformat.format(Date(millisUntilFinished))
                    }

                }
            }
            mCountDownTimer?.start()
        }
    }

}